//
//  MyViewController.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/18.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class MyViewController: UIViewController,MyCellDelegate,SettingViewControllerDelegate,QQToolDelegate,WXToolDelegate {
    
    ///登陆视图
    var vi = LoginView()
    
    //表格
    var tableView = UITableView()
    
    
    internal func shareSuccssWithQQCode(str: NSInteger) {
        
    }

    //QQ工具类
    let qqT = QQTool()
    //微信工具类
    let wxT = WXTool()
    

    //右边navigationitem按钮
    lazy var leftBarItemBtn : BlockBtn = {
        
        let leftBarItemBtn = BlockBtn(type: UIButtonType.custom)
        
        leftBarItemBtn.backgroundImage(for: .normal)
        
        //正常和高亮状态设置为同一张照片
        leftBarItemBtn.setImage(UIImage.init(named: "ico"), for: .normal)
        leftBarItemBtn.setImage(UIImage.init(named: "ico"), for: .highlighted)
        leftBarItemBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 40)
        leftBarItemBtn.setTitle("BlockButton", for: UIControlState())
        
        //添加事件
        leftBarItemBtn.setClickClosure { (btn) in
            print("单机了")
        }
        return leftBarItemBtn
    }()
    
    //表格数据源
    lazy var dataArr : NSMutableArray = {
        var data = NSMutableArray()
        data = ["交易明细","中奖纪录","兑换纪录","分享有礼","设置"]
        return data
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        
        //标题
        self.navigationItem.title = "我的"
        
        //判断本地是否有用户标识
        guard ((localSave.object(forKey: userToken) as? String) != nil) else {

            let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
            tableView = UITableView(frame: rect, style: .plain)
            tableView.dataSource = self
            tableView.delegate = self
            view.addSubview(tableView)
            //加载未登陆视图
            setUnlogin()
            return
        }
        

        //设置表格
        setUpTablle()

    }
}


// MARK:- 设置表格
extension MyViewController {
    func setUpTablle() -> Void {
//        print("dddddd")
        
        tableView = UITableView.init(frame: self.view.bounds, style: .plain)

        tableView.dataSource = self
        tableView.delegate = self
        view.addSubview(tableView)
    }
}

// MARK:- 未登陆视图
extension MyViewController {
    
    
    func setUnlogin() -> Void {
        
        vi = LoginView()
//        vi.delegate = self
        vi.view.frame = CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 64)
        self.present(vi, animated: true, completion: nil)
    }
}

// MARK:- 登陆成功后隐藏左上角按钮并且将导航栏改变为白色
extension MyViewController {
    func logoutSucceViewStyle() -> Void {
        //修改导航栏背景色
        self.navigationController?.navigationBar.barTintColor =
            UIColor.white
    }
}


// MARK:- 表格代理和方法
extension MyViewController :  UITableViewDelegate,UITableViewDataSource {
    
    // MARK:- 单机事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 && indexPath.row == 4 {
            let setVC = SettingViewController()
            setVC.delegate = self
            self.navigationController?.pushViewController(setVC, animated: true)
        } else {
            MBManager.showBriefAlert("正在开发")
        }
        
    }
    
    // MARK: - Table view data source
    
    func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        
        return 2
    }
    
    //多少行在每组中
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        switch section {
        case 0:
            return 2
        default:
            return dataArr.count
        }
    }
    
    //设置section 间距
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 1
    }
    
    
    //设置行高
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if indexPath.section == 0 && indexPath.row == 0{
            return 150
        } else if indexPath.section == 0 && indexPath.row == 1 {
            return 55
        } else {
            return 45
        }

    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        

        var cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as? MyCell
        
        if cell == nil {
            cell = MyCell(style: .default, reuseIdentifier: "cellID")
            tableView.separatorStyle = UITableViewCellSeparatorStyle.none
            
            //继承代理
            cell?.delegate = self
        }
        
        //根据状态隐藏
        switch indexPath.section {
        case 0:
            cell?.imgView.isHidden = false
            cell?.downNameLabel.isHidden = true
            cell?.qianzhiImg.isHidden = true
            cell?.line.isHidden = true
            cell?.disclosureImg.isHidden = true
            cell?.oneViewBackView.isHidden = true
            cell?.oneViewFrontView.isHidden = true

        default:
            cell?.imgView.isHidden = true
            cell?.bgImg.isHidden = true

        }
        
        if indexPath.section == 0 && indexPath.row == 1 {
            cell?.imgView.isHidden = true
            cell?.nameLabel.isHidden = true
            cell?.bgImg.isHidden = true
            cell?.oneViewBackView.isHidden = false
            cell?.oneViewFrontView.isHidden = false
        }
        
        if indexPath.section == 1 {
            cell?.imgView.isHidden = true
            cell?.nameLabel.isHidden = true
            cell?.downNameLabel.text = self.dataArr[indexPath.row] as? String
            cell?.qianzhiImg.image = UIImage.init(named: "ico")
            cell?.oneViewBackView.isHidden = true
            cell?.oneViewFrontView.isHidden = true
        }
       
        return cell!
    }
}

// MARK:- 样式设置
extension MyViewController {
    func setStyle() -> Void {
        //设置样式组
        tableView.tableFooterView = UIView()
        tableView.backgroundColor = UIColor.gray
        //去除分割线
        
    }
}

// MARK:- 接收代理
extension MyViewController {
    //跳转积分界面
    func jumpToAccout() {
        print("挑了")
        
        self.navigationController?.pushViewController(AccountTableViewController(), animated: true)
    }
    
    //跳转钱包界面
    func jumpToCash() {
//        self.navigationController?.pushViewController(UIViewController(), animated: true)
    }
    
    func selePhoto() {

    }
    
    func logSuccess() {
        self.navigationItem.title = "我的"
        leftBarItemBtn.isHidden = true
        
        //修改导航栏背景色
        self.navigationController?.navigationBar.barTintColor =
            UIColor.orange

        vi.dismiss(animated: true, completion: nil)

        //登陆后加载表格
        let rect = CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        tableView = UITableView(frame: rect, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        view.addSubview(tableView)
    }
    
    //跳转忘记密码页面
    func forVC() {
        self.navigationController?.pushViewController(ForgetViewController(), animated: true)
    }
    
    //跳转注册页面
    func rigVC() {
        self.navigationController?.pushViewController(RigisterViewController(), animated: true)
    }
    
    //微信登陆
    func WXVC() {
        print("选择微信登陆")
        //FIXME:微信回调不行
//        wxT.clickAuto()
        
        wxT.clickAuto { (result) in
            if result == "授权成功" {
                
            }
        }
        wxT.delegate = self
    }
    
    //QQ登陆
    func QQVC() {
        print("选择QQ登陆")
        
        qqT.qqLogin()
        qqT.delegate = self
    }
    //显示登陆界面
    func back() {

        self.setUnlogin()
        self.logoutSucceViewStyle()
        
        tableView.isHidden = true
    }

    //QQ登陆成功回调
    func qqLoginCallBack() {
        print("QQ登陆成功回调")
        vi.dismiss(animated: true, completion: nil)
        //处理导航栏状态
        self.logSuccess()
        
        
    }
    
    //FIXME:无法回调
    //微信登陆成功回调
    func WXLoginCallBack() {
        print("hhhh")
        print("微信登陆成功回调")
    }

}





